In [2]:
import pandas as pd
import numpy as np
import matplotlib as plt 
import seaborn as sns
import plotly.express as px
In [3]:
amzn = pd.read_csv('/home/developer/Downloads/AMZN.csv')
In [4]:
amzn
Out[4]:
Date Open High Low Close Adj Close Volume
0 1997-05-15 0.121875 0.125000 0.096354 0.097917 0.097917 1443120000
1 1997-05-16 0.098438 0.098958 0.085417 0.086458 0.086458 294000000
2 1997-05-19 0.088021 0.088542 0.081250 0.085417 0.085417 122136000
3 1997-05-20 0.086458 0.087500 0.081771 0.081771 0.081771 109344000
4 1997-05-21 0.081771 0.082292 0.068750 0.071354 0.071354 377064000
... ... ... ... ... ... ... ...
6511 2023-03-30 101.550003 103.040001 101.010002 102.000000 102.000000 53633400
6512 2023-03-31 102.160004 103.489998 101.949997 103.290001 103.290001 56704300
6513 2023-04-03 102.300003 103.290001 101.430000 102.410004 102.410004 41135700
6514 2023-04-04 102.750000 104.199997 102.110001 103.949997 103.949997 48662500
6515 2023-04-05 103.910004 103.910004 100.750000 101.099998 101.099998 45103000

6516 rows × 7 columns

In [5]:
amzn.isnull().sum()
Out[5]:
Date         0
Open         0
High         0
Low          0
Close        0
Adj Close    0
Volume       0
dtype: int64
In [6]:
amzn['Date'] = pd.to_datetime(amzn['Date'])
In [7]:
# set_index
amzn.set_index('Date', inplace=True)
In [8]:
amzn
Out[8]:
Open High Low Close Adj Close Volume
Date
1997-05-15 0.121875 0.125000 0.096354 0.097917 0.097917 1443120000
1997-05-16 0.098438 0.098958 0.085417 0.086458 0.086458 294000000
1997-05-19 0.088021 0.088542 0.081250 0.085417 0.085417 122136000
1997-05-20 0.086458 0.087500 0.081771 0.081771 0.081771 109344000
1997-05-21 0.081771 0.082292 0.068750 0.071354 0.071354 377064000
... ... ... ... ... ... ...
2023-03-30 101.550003 103.040001 101.010002 102.000000 102.000000 53633400
2023-03-31 102.160004 103.489998 101.949997 103.290001 103.290001 56704300
2023-04-03 102.300003 103.290001 101.430000 102.410004 102.410004 41135700
2023-04-04 102.750000 104.199997 102.110001 103.949997 103.949997 48662500
2023-04-05 103.910004 103.910004 100.750000 101.099998 101.099998 45103000

6516 rows × 6 columns

In [9]:
# Data Analysis and Visualization
In [10]:
# How can I visualize the stock's closing prices over time?
sns.lineplot(amzn, x='Date', y='Close')
Out[10]:
<Axes: xlabel='Date', ylabel='Close'>
No description has been provided for this image
In [11]:
# How do I calculate and plot moving averages to identify trends?
# amzn['axis_y']=amzn['Open'].mean()
# sns.lineplot(amzn, x='Date', y='axis_y')
In [12]:
# How can I visualize and analyze trading volume trends?
sns.lineplot(amzn, x='Date', y='Volume')
Out[12]:
<Axes: xlabel='Date', ylabel='Volume'>
No description has been provided for this image
In [13]:
amzn['Volume_MA_30'] = amzn['Volume'].rolling(window=30).mean()
sns.lineplot(amzn, x='Date', y='Volume_MA_30')
Out[13]:
<Axes: xlabel='Date', ylabel='Volume_MA_30'>
No description has been provided for this image
In [14]:
amzn
Out[14]:
Open High Low Close Adj Close Volume Volume_MA_30
Date
1997-05-15 0.121875 0.125000 0.096354 0.097917 0.097917 1443120000 NaN
1997-05-16 0.098438 0.098958 0.085417 0.086458 0.086458 294000000 NaN
1997-05-19 0.088021 0.088542 0.081250 0.085417 0.085417 122136000 NaN
1997-05-20 0.086458 0.087500 0.081771 0.081771 0.081771 109344000 NaN
1997-05-21 0.081771 0.082292 0.068750 0.071354 0.071354 377064000 NaN
... ... ... ... ... ... ... ...
2023-03-30 101.550003 103.040001 101.010002 102.000000 102.000000 53633400 5.738841e+07
2023-03-31 102.160004 103.489998 101.949997 103.290001 103.290001 56704300 5.740058e+07
2023-04-03 102.300003 103.290001 101.430000 102.410004 102.410004 41135700 5.677079e+07
2023-04-04 102.750000 104.199997 102.110001 103.949997 103.949997 48662500 5.650686e+07
2023-04-05 103.910004 103.910004 100.750000 101.099998 101.099998 45103000 5.602582e+07

6516 rows × 7 columns

In [15]:
# What are the correlations between different stock features, and how can I visualize them?
orrelation_matrix = amzn[['Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume']].corr()
sns.heatmap(orrelation_matrix, annot=True, cmap='coolwarm', linewidths=0.5)
Out[15]:
<Axes: >
No description has been provided for this image
In [16]:
# How can I calculate and plot daily returns to analyze stock volatility?
amzn['Daily Return'] = amzn['Close'].pct_change()
In [ ]:
sns.histplot(amzn)
In [4]:
# How can I use interactive visualization libraries to create dynamic and interactive charts?
fig = px.line(amzn, x='Date', y='Close')
fig.show()
In [8]:
import matplotlib.pyplot as plt
import seaborn as sns

# Plot the 'Close' price over time
plt.figure(figsize=(14, 7))
sns.lineplot(x=amzn['Date'], y=amzn['Close'])
plt.title('Amazon Close Prices Over Time')
plt.xlabel('Date')
plt.ylabel('Close Price')
plt.show()
No description has been provided for this image
In [9]:
# Calculate 30-day moving average of 'Close' price
amzn['Close_MA_30'] = amzn['Close'].rolling(window=30).mean()

# Plot the 'Close' price and 30-day moving average
plt.figure(figsize=(14, 7))
sns.lineplot(x=amzn['Date'], y=amzn['Close'], label='Close')
sns.lineplot(x=amzn['Date'], y=amzn['Close_MA_30'], label='30-Day MA')
plt.title('Amazon Close Prices and 30-Day Moving Average')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.show()
No description has been provided for this image
In [10]:
# Calculate 30-day moving average of 'Close' price
amzn['Close_MA_30'] = amzn['Close'].rolling(window=30).mean()

# Plot the 'Close' price and 30-day moving average
plt.figure(figsize=(14, 7))
sns.lineplot(x=amzn['Date'], y=amzn['Close'], label='Close')
sns.lineplot(x=amzn['Date'], y=amzn['Close_MA_30'], label='30-Day MA')
plt.title('Amazon Close Prices and 30-Day Moving Average')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
plt.show()
No description has been provided for this image
In [11]:
# Calculate correlation matrix
correlation_matrix = amzn[['Close', 'Volume', 'Close_MA_30']].corr()
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')
plt.title('Correlation Matrix')
plt.show()
No description has been provided for this image
In [16]:
 
  Cell In[16], line 1
    jupyter nbconvert --to html your_notebook.ipynb
            ^
SyntaxError: invalid syntax
In [13]:
def calculate_moving_average(data, window):
    """
    Calculate the moving average of a given data series.

    Parameters:
    data (pd.Series): The data series to calculate the moving average on.
    window (int): The window size for the moving average.

    Returns:
    pd.Series: The moving average of the data series.
    """
    return data.rolling(window=window).mean()
In [14]:
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px

# Initialize Dash app
app = dash.Dash(__name__)

# Create a Plotly figure
fig = px.line(amzn, x='Date', y='Close', title='Interactive Line Plot of Close Prices')

# Define layout
app.layout = html.Div([
    dcc.Graph(figure=fig)
])

# Run app
if __name__ == '__main__':
    app.run_server(debug=True)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[14], line 1
----> 1 import dash
      2 import dash_core_components as dcc
      3 import dash_html_components as html

ModuleNotFoundError: No module named 'dash'
In [ ]: